#ifndef TRIQUAD_H
#define TRIQUAD_H

#include <QObject>
#include <QVector>
#include <QGenericMatrix>
#include <QVector3D>
#include <QVector2D>

#define CIRCLE TriQuad::makeQuadric(1.0, 1.0, 0.0, 0.0, 0.0, -0.3)

class TriQuad : public QObject
{
    Q_OBJECT

    QVector3D vertices[3];
    QMatrix3x3 quads[3];

    QVector<QVector2D> points;
    bool changed;

public:
    explicit TriQuad(QObject *parent = 0);
    explicit TriQuad(const QVector2D &v1,
                     const QVector2D &v2,
                     const QVector2D &v3,
                     QObject *parent = 0);
    explicit TriQuad(const TriQuad& tq, QObject *parent = 0);

    const QVector3D& v(int i)const;
    QVector3D& v(int i);
    QVector2D vTo2DAffine(int i) const;
    const QMatrix3x3& q(int i)const;
    QMatrix3x3& q(int i);

    const TriQuad& operator=(const TriQuad& tq);

    static QMatrix3x3 makeQuadric(float x2, float y2, float xy, float x, float y, float c);

    QVector<QVector2D> getTriangleLines() const;
    QVector<QVector2D> getCurveLines();
    QVector<QVector2D> getPointCloud();

    float eval(const QVector2D& p)const;
    

private:
    QVector3D bariCoord(const QVector2D &p)const;
    
};

template<class T>
T affineSum(float t, T a, T b)
{
    return (1-t)*a + t*b;
}

template<class T>
T affineSum(T a, T b, T c, const QVector3D& t)
{
    return t.x()*a + t.y()*b + t.z()*c;
}

#endif // TRIQUAD_H
